package com.ruoyi.combat.service.userlocation.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.combat.domain.camera.CombatCameraInfo;
import com.ruoyi.combat.domain.userlocation.*;
import com.ruoyi.combat.mapper.userlocation.*;
import com.ruoyi.combat.service.userlocation.ICombatUserService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class CombatUserServiceImpl implements ICombatUserService
{
    @Autowired
    private CombatUserMapper combatUserMapper;
    @Autowired
    private CombatBlogMapper combatBlogMapper;
    @Autowired
    private CombatWarnMapper combatWarnMapper;
    @Autowired
    private CombatLocationTaskMapper combatLocationTaskMapper;
    @Autowired
    private CombatRelationMapper combatRelationMapper;

    /**
     * 查询用户信息
     * 
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public CombatUser selectCombatUserById(Long id)
    {
        return combatUserMapper.selectCombatUserById(id);
    }

    /**
     * 根据用户社交id查询用户信息
     *
     * @param userId 用户社交id
     * @return 用户信息
     */
    public CombatUser selectCombatUserByUserId(String userId){
        return combatUserMapper.selectCombatUserByUserId(userId);
    }
    /**
     * 根据城市和重保用户id查询非重报用户
     *
     * @param combatUser
     * @return 用户信息
     */
    public List<CombatUser> selectCombatUserByCity(CombatUser combatUser){
        return combatUserMapper.selectCombatUserByCity(combatUser);
    }

    /**
     * 查询用户信息列表
     * 
     * @param combatUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<CombatUser> selectCombatUserList(CombatUser combatUser)
    {
        return combatUserMapper.selectCombatUserList(combatUser);
    }

    /**
     * 新增用户信息
     * 
     * @param combatUser 用户信息
     * @return 结果
     */
    @Override
    public int insertCombatUser(CombatUser combatUser)
    {
        return combatUserMapper.insertCombatUser(combatUser);
    }

    /**
     * 修改用户信息
     * 
     * @param combatUser 用户信息
     * @return 结果
     */
    @Override
    public int updateCombatUser(CombatUser combatUser)
    {
        return combatUserMapper.updateCombatUser(combatUser);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatUserByIds(Long[] ids)
    {
        for (Long id : ids){
            //删除该用户所发布的所有博客
            CombatUser combatUser = combatUserMapper.selectCombatUserById(id);
            String userId = combatUser.getUserId();
            List<CombatBlog> blogsByUserId = combatBlogMapper.getBlogsByUserId(userId);
            for (CombatBlog blog : blogsByUserId) {
                combatBlogMapper.deleteCombatBlogById(blog.getId());
            }
            //删除相关泄露警告
            CombatWarn combatWarn = new CombatWarn();
            combatWarn.setUserid(userId);
            List<CombatWarn> combatWarns = combatWarnMapper.selectCombatWarnList(combatWarn);
            for (CombatWarn warn : combatWarns) {
                combatWarnMapper.deleteCombatWarnById(warn.getId());
            }
            //删除相关抗定位任务
            CombatLocationTask combatLocationTask = new CombatLocationTask();
            combatLocationTask.setUserId(userId);
            List<CombatLocationTask> combatLocationTasks = combatLocationTaskMapper.selectCombatLocationTaskList(combatLocationTask);
            for (CombatLocationTask task : combatLocationTasks){
                combatLocationTaskMapper.deleteCombatLocationTaskById(task.getId());
            }
            //删除用户关系
            CombatRelation relation = new CombatRelation();
            relation.setUserid(userId);
            List<CombatRelation> combatRelations = combatRelationMapper.selectRelationByUserId(relation);
            for (CombatRelation rel : combatRelations){
                combatRelationMapper.deleteCombatRelationById(rel.getId());
            }
        }
        return combatUserMapper.deleteCombatUserByIds(ids);
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatUserById(Long id)
    {
        return combatUserMapper.deleteCombatUserById(id);
    }

    @Override
    public String importUserInfo(List<CombatUser> list) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CombatUser user : list) {
            try {
                /// user的手机号 用户类型为空 账号id 家庭住址经度 家庭住址纬 工作地址纬度 工作地址纬度 任意为空不可插入数据
                if (StringUtils.isEmpty(user.getUserId()) || StringUtils.isEmpty(user.getUserPhone()) ||
                        StringUtils.isEmpty(user.getUserType()) || StringUtils.isEmpty(user.getUserHomeLat()) ||
                        StringUtils.isEmpty(user.getUserHomeLon()) || StringUtils.isEmpty(user.getUserWorkLon()) ||
                        StringUtils.isEmpty(user.getUserWorkLat())) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、用户 " + user.getUserId() + " 导入失败：数据格式不正确";
                    failureMsg.append(msg);
                    continue;
                }
                // 判断这个user是否已经存在查表判断
                CombatUser userd =  combatUserMapper.selectCombatUserByUserId(user.getUserId());
                if (userd != null) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、用户id" + user.getUserId() + " 已存在";
                    failureMsg.append(msg);
                    continue;
                }
                CombatUser userdd =  combatUserMapper.selectCombatUserByPhone(user.getUserId());
                if (userdd != null) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、用户手机号" + user.getUserId() + " 已存在";
                    failureMsg.append(msg);
                    continue;
                }
                combatUserMapper.insertCombatUser(user);
                successNum++;
                String msg = "<br/>" + successNum + "、用户 " + user.getUserId() + " 导入成功";
                successMsg.append(msg);
            }  catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、用户 " + user.getUserId() + " 导入失败：";
                if (StringUtils.isNotEmpty(e.getMessage())) {
                    msg += e.getMessage();
                } else {
                    msg += "未知错误";
                }
                failureMsg.append(msg);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
